﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Data;

public class ShowStuProInfo : MonoBehaviour {

    public string url = "http://localhost/sell/showStuInfo.php";
    string action, velue,data;
    StuInfo _info;
    public Text tipInfo;
    public InputField inputId;
    public InputField inputName;
    public InputField inputProid;
    public InputField tipUserInfo;
    public GameObject modifyBtnsPlane;
    public GameObject modifyProidPlane;
    public GameObject modifyTimePlane;
    public GameObject modifyBetaPlane;
    public InputField inputProId;
    public InputField inputTime;
    public InputField inputBeta;

    DataSet ds;
    int id;
    string proid;
    string checkid;
    string names;
    string phone, beta;
    int time,num;
    public string s1 = "select * from stu_info where Id = '";
    public string s2 = " 序列号： ";
    public string s3 = " 验证码： ";
    public string s4 = " 信息不存在！请重新输入 ";
    public string s5 = "编号不能为空，请输入编号";
    public string s6 = "select * from stu_info where productid = '";
    public string s7 = "序列号输入有误，请重新输入";
    public string s8 = "编号： ";
    public string s9 = "stu_info";
    public string s10 = "productid";
    public string s11 = "Id";
    public string s12 = "序列号修改成功！";
    public string s13 = "验证码输入有误，请重新输入";
    public string s14 = "验证码修改成功！";
    public string s15 = "userid";
    public string s16 = " 客户名：";
    public string s17 = " 手机号：";
    public string s18 = " 次数：";
    public string s19 = " 版本号：";
    public string s20 = "select * from stu_info where username = '";
    public string s21 = "用户名输入不正确，请重新输入";
    public string s22 = "time";
    public string s23 = "次数修改成功！";
    public string s24 = "beta";
    void Start()
    {
        modifyBtnsPlane.SetActive(false);
        modifyProidPlane.SetActive(false);
        modifyTimePlane.SetActive(false);
        modifyBetaPlane.SetActive(false);
    }

    void ShowTip(string ss)
    {
        tipInfo.text = ss;
    }

    void ShowInfo()
    {
        tipUserInfo.text = s8 + id + s2 + proid + s3 + checkid + s16 + names + s17 + phone + s18 + time + s19 + beta;
        modifyBtnsPlane.SetActive(true);
    }

    public void CheckId()
    {
        if (inputId.text != "")
        {
            action = "1";
            velue = inputId.text;
            StartCoroutine(SentMessage());//调用协同程序 进行数据交互
        }
        else
        {
            ShowTip(s5);
        }
    }

    public void CheckName()
    {
        if (inputName.text.Length != 0)
        {
            action = "2";
            velue = inputName.text;
            StartCoroutine(SentMessage());//调用协同程序 进行数据交互
        }
        else
        {
            ShowTip(s21);
        }
    }

    public void CheckProid()
    {
        if (inputProid.text.Length == 16)
        {
            action = "3";
            velue = inputProid.text;
            StartCoroutine(SentMessage());//调用协同程序 进行数据交互

        }
        else
        {
            ShowTip(s7);
        }
    }

    public void ModifyTime()
    {
        modifyTimePlane.SetActive(true);
    }

    public void ModifyBeta()
    {
        modifyBetaPlane.SetActive(true);
    }

    public void SaveTime()
    {
        time = int.Parse(inputTime.text);
        if (time >= 0 && time < 4)
        {
            Debug.Log("time:" + time);
            action = "4";
            velue = inputTime.text;
            id = _info.Id;
            StartCoroutine(SentMessage());//调用协同程序 进行数据交互

        }
        else
        {
            ShowTip("次数输入不正确，请重新输入！");
        }
    }

    public void SaveBeta()
    {
        if (inputBeta.text.Length > 3)
        {
            beta = inputBeta.text;
            action = "5";
            velue = beta;
            id = _info.Id;
            StartCoroutine(SentMessage());//调用协同程序 进行数据交互
        }
        else
        {
            ShowTip("版本号输入不正确，请重新输入！");
        }
    }
    //利用协同程序 发送数据到PHP服务器，并接收和处理服务器返回的数据
    IEnumerator SentMessage()
    {
        #region 发送POST数据
        WWWForm form = new WWWForm();
        form.AddField("action", action);
        form.AddField("velue", velue);
        form.AddField("uid", id);
        WWW www = new WWW(url, form);
        #endregion
        #region 接收和处理服务器端返回的单个字符串数据
        yield return www;
        if (www.error != null)
        {
            Debug.LogError(www.error);
        }
        else
        {
            data = www.text;
            Debug.Log("data：" + data);
            ReadData();
        }
        #endregion
    }

    void ReadData()
    {
        Debug.Log("num：" + data.Length);
        num = data.Length;
        if (num < 5)
        {
            modifyBtnsPlane.SetActive(false);
            tipUserInfo.text = "";
            if (data.CompareTo("1") == 1)
            {
                ShowTip("产品编号不存在，请重新输入！");
                inputId.text = "";
            }
            if (data.CompareTo("2") == 1)
            {
                ShowTip("用户名不存在，请重新输入！");
                inputName.text = "";
            }
            if (data.CompareTo("3") == 1)
            {
                ShowTip("序列号不存在，请重新输入");
                inputProid.text = "";
            }
            if (data.CompareTo("4") == 1)
            {
                ShowTip("次数修改成功！");
                inputTime.text = "";
                modifyTimePlane.SetActive(false);
                ShowInfo();
            }
            if (data.CompareTo("5") == 1)
            {
                ShowTip("版本号修改成功！");
                inputBeta.text = "";
                modifyBetaPlane.SetActive(false);
                ShowInfo();
            }
        }
        else
        {
            //_info = JsonUtility.FromJson<StuInfo>(data);
            if (_info.phone != null)
            {
                id = _info.Id;
                proid = _info.proid;
                checkid = _info.checkid;
                names = _info.names;
                phone = _info.phone;
                time = _info.times;
                beta = _info.beta;
                ShowInfo();
            }
            else
                ShowTip("服务器繁忙，请稍后再试！");
        }
    }
}
